package com.munhrasoft.elearning.managedbeans;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.primefaces.event.SelectEvent;

import com.munhrasoft.elearning.business.entities.CourseClass;
import com.munhrasoft.elearning.business.entities.Courses;
import com.munhrasoft.elearning.business.entities.Exercises;
import com.munhrasoft.elearning.business.entities.Questions;
import com.munhrasoft.elearning.business.entities.Students;
import com.munhrasoft.elearning.business.logic.CoursesEJB;
import com.munhrasoft.elearning.business.logic.StudentsEJB;

@ManagedBean
@SessionScoped
public class StudentMainController {

    private static Logger   logger = Logger.getLogger(StudentMainController.class.getName());

    @EJB
    private CoursesEJB      coursesEJB;
    private List<Courses>   coursesList;
    private Courses         selectedCourse;
    private CourseClass     selectedClass;
    private Exercises       selectedExercise;
    private Questions       selectedQuestion;

    @ManagedProperty(value = "#{loginController}")
    private LoginController loginController;

    @PostConstruct
    public void init() {
        // null pointer happens here as loginController returned null
        // there was a fail on the injection
        coursesList = loginController.getStudent().getCourses();
    }

    public List<Courses> getCoursesList() {
        return coursesList;
    }

    public void setCoursesList(List<Courses> coursesList) {
        this.coursesList = coursesList;
    }

    public Courses getSelectedCourse() {
        return selectedCourse;
    }

    public void setSelectedCourse(Courses selectedCourse) {
        this.selectedCourse = selectedCourse;
    }

    public CourseClass getSelectedClass() {
        return selectedClass;
    }

    public void setSelectedClass(CourseClass selectedClass) {
        this.selectedClass = selectedClass;
    }

    public Exercises getSelectedExercise() {
        return selectedExercise;
    }

    public void setSelectedExercise(Exercises selectedExercise) {
        this.selectedExercise = selectedExercise;
    }

    public Questions getSelectedQuestion() {
        return selectedQuestion;
    }

    public void setSelectedQuestion(Questions selectedQuestion) {
        this.selectedQuestion = selectedQuestion;
    }

    public void rowSelect(SelectEvent event) {
        logger.info("Row select");
    }

    public String goCourse() {
        return "courseclasses";
    }

    public String goClass() {
        return "goclass";
    }

    public String goExercises() {
        return "goexercises";
    }

    public String goText() {
        return "gotext";
    }

    public String goQuestions() {
        return "goquestion";
    }

    public LoginController getLoginController() {
        return loginController;
    }

    public void setLoginController(LoginController loginController) {
        this.loginController = loginController;
    }

    public void answerQuestion() {
        // Check if the answer is correct

        Students student = loginController.getStudent();

        if (selectedQuestion == null) {
            FacesContext.getCurrentInstance().addMessage("answer",
                    new FacesMessage("No answer selected !"));
            return;
        } else if (!selectedQuestion.isCorrect()) {
            FacesContext.getCurrentInstance().addMessage("answer",
                    new FacesMessage("The selected answer is wrong !"));
            return;
        } else {
            FacesContext.getCurrentInstance().addMessage("answer",
                    new FacesMessage("The selected answer is CORRECT !!!"));
            selectedQuestion.getStudentsThatAnswr().add(student);
        }
    }

}
